package cpr.utils;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import cpr.Rule;

public class SizeOf {
    public static int sizeOf(Object o) {
    	if (o == null) {
    		return 4;
    	} else if (o instanceof String) {
    		String s = (String)o;
    		return s.length();
    	} else if (o instanceof Integer) {
    		return 4;
    	} else if (o instanceof Rule) {
    		Rule r = (Rule)o;
    		int s = sizeOf(r.attrName);
    		s +=sizeOf(r.attrValue);
    		s +=sizeOf(r.attrValueDigital);
    		s +=sizeOf(r.ruleNr);
    		s +=sizeOf(r.hl);
    		s +=sizeOf(r.hl_digital);
    		s +=sizeOf(r.hl_file_values);
    		s +=sizeOf(r.score);
    		return s;
    	} else if (o instanceof String[]) {
    		String[] sa = (String[])o;
    		int s =0;
    		for (int i = 0; i < sa.length; i++) {
				s+=sizeOf(sa[i]);
			}
    		return s;
    	} else if (o instanceof int[]) {
    		int[] sa = (int[])o;
    		int s =0;
    		for (int i = 0; i < sa.length; i++) {
				s+=sizeOf(sa[i]);
			}
    		return s;
    	} else if (o instanceof Collection) {
        	int s = 0;
        	Collection aSet = (Collection)o;
        	for (Iterator iter = aSet.iterator(); iter.hasNext();) {
    			Object key = iter.next();
    			s +=sizeOf(key);
    		}
        	return s;
    	} else if (o instanceof Map) {
    		Map aMap = (Map) o;
        	int s = 0;
        	for (Iterator iter = aMap.keySet().iterator(); iter.hasNext();) {
    			Object key = iter.next();
    			Object value = aMap.get(key);
    			s +=sizeOf(key);
    			s +=sizeOf(value);
    		}
        	return s;

    	} else {
    		throw new RuntimeException("unknown type "+o);
    	}
    	
    }
    public static int sizeOf(int i) {
    	return 4;
    }

}
